import { start as startNodeAuth, StartOptions } from 'maishu-node-auth';
import { AuthDataContext } from 'maishu-node-auth/out/data-context';
import path = require("path");

export function startNodeAuthServerSide(config: {
    bindIP?: string, port: number, db: StartOptions["db"],
    proxy?: StartOptions["proxy"], initDatabase?: boolean,
    permissions?: StartOptions["permissions"]
}) {
    let options: StartOptions = {
        bindIP: config.bindIP,
        port: config.port,
        db: config.db,
        controllersDirectory: path.join(__dirname, "controllers"),
        entitiesDirectory: path.join(__dirname, "entities"),
        proxy: config.proxy,
        permissions: config.permissions,
        headers: {
            'Content-Type': 'application/json;charset=utf-8',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'delete, *',
            'Access-Control-Allow-Headers': 'Content-Type, application-id, token, UserId, *'
        }
        // actionFilters: [(req, res) => {
        //     res.setHeader("Access-Control-Allow-Origin", "*");
        //     res.setHeader("Access-Control-Allow-Headers", "Content-Type, *");
        //     res.setHeader("Access-Control-Allow-Methods", "*");

        //     return null;
        // }]
    }

    if (config.initDatabase) {
        options.initDatabase = async (dc) => initDatabase(dc)
    }

    startNodeAuth(options)

}

async function initDatabase(dc: AuthDataContext) {
    await dc.initDatabase("18502146746", "22");
    await dc.appendPathsToResource([
        "/AdminSite/*",
        "/AdminStock/*",
        "/AdminShop/*",
        "/AdminMember/*",
        "/AdminWeiXin/*",
        "/AdminAccount/*",
        "/UserSite/*",
        "/UserStock/*",
        "/UserShop/*",
        "/UserMember/*",
        "/UserWeiXin/*",
        "/UserAccount/*",
        "/image/*"
    ], dc.baseModuleResourceId);

    const homeResourceId = "B87995FF-C463-4BB4-9451-2F1A4BB89AD7";
    let menuItem = await dc.resources.findOne({ id: homeResourceId });
    if (menuItem == null) {
        await dc.createMenuItem({
            id: homeResourceId, name: "首页", icon: "icon-dashboard",
            url: "#shop/home/index", sortNumber: 2000
        })
    }

    //=========================================================================

    const storeResourceId = "20DD9F33-AED0-477A-AC51-ED46FF5C34B9";
    menuItem = await dc.resources.findOne({ id: storeResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: storeResourceId, name: "微店", icon: "icon-home",
            sortNumber: 2100
        })
    }

    const storeSettingResourceId = "DD899E8D-4943-4CA0-8839-2ED0C60B7413";
    menuItem = await dc.resources.findOne({ id: storeSettingResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: storeSettingResourceId, name: "店铺设置", icon: "icon-cog",
            sortNumber: 2110, url: "#station_index", parentId: storeResourceId,
        })
    }

    const storeHomeResourceId = "21484829-2405-497B-B5DE-A889C08FB9BD";
    menuItem = await dc.resources.findOne({ id: storeHomeResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: storeHomeResourceId, name: "店铺首页", icon: "icon-flag",
            sortNumber: 2120, url: "#station_store_home", parentId: storeResourceId,
        })
    }

    const memberHomeResourceId = "D6CE4649-D59F-483D-9778-6F9D6134422A";
    menuItem = await dc.resources.findOne({ id: memberHomeResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: memberHomeResourceId, name: "会员主页", icon: "icon-tint",
            sortNumber: 2130, url: "#station_store_member", parentId: storeResourceId,
        })
    }

    const storeMenuResourceId = "46848FC9-0585-4AAC-82E4-5CDE4C2F6CCE";
    menuItem = await dc.resources.findOne({ id: storeMenuResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: storeMenuResourceId, name: "店铺导航", icon: "icon-move",
            sortNumber: 2140, url: "#station_store_menu", parentId: storeResourceId,
        })
    }

    let storeShoppingCartResourceId = "0DC9D0A1-F370-4DE8-A90F-77D377AFE605";
    menuItem = await dc.resources.findOne(storeShoppingCartResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: storeShoppingCartResourceId, name: "购物车", icon: "icon-shopping-cart",
            sortNumber: 2150, url: "#station_store_shoppingCart", parentId: storeResourceId
        })
    }

    let storeStyleResourceId = "3594CED5-060A-4A50-9D52-36C8487E689B";
    menuItem = await dc.resources.findOne(storeStyleResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: storeStyleResourceId, name: "店铺风格", icon: "icon-bookmark",
            sortNumber: 2160, url: "#station_store_style", parentId: storeResourceId
        })
    }

    const goodsCategoryResourceId = "2F80C0BB-7432-4E64-8176-ABAB0B066351";
    menuItem = await dc.resources.findOne(goodsCategoryResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: goodsCategoryResourceId, name: "商品类别", icon: "station_store_style",
            sortNumber: 2170, url: "#station_store_categories", parentId: storeResourceId
        })
    }

    const pageListResourceId = "7089DA5E-C81E-4C43-B891-2300C1A891C7";
    menuItem = await dc.resources.findOne(pageListResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: pageListResourceId, name: "页面列表", icon: "icon-list-alt",
            sortNumber: 2180, url: "#station_pageList", parentId: storeResourceId
        })
    }


    //=========================================================================

    const goodsResourceId = "B773DB1A-DAED-487F-9461-96C6AD425094";
    menuItem = await dc.resources.findOne({ id: goodsResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: goodsResourceId, name: "商品", icon: "icon-gift",
            sortNumber: 2200
        })
    }

    const goodsListResourceId = "21F400B4-79AD-4221-AE15-3EFC0E074FE6";
    menuItem = await dc.resources.findOne(goodsListResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: goodsListResourceId, name: "商品列表", icon: "icon-list-ul",
            sortNumber: 2210, parentId: goodsResourceId, url: "#shopping_productList"
        })
    }

    const goodsBrandResourceId = "8620E392-E80E-4547-AF4D-4F3EB2BB4B60";
    menuItem = await dc.resources.findOne(goodsBrandResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: goodsBrandResourceId, name: "商品品牌", icon: "icon-trophy",
            sortNumber: 2220, parentId: goodsResourceId, url: "#shopping_brandList"
        })
    }

    const goodsTemplateResourceId = "AD60009E-EB35-4E90-BAEA-34C515A0560F";
    menuItem = await dc.resources.findOne(goodsTemplateResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: goodsTemplateResourceId, name: "商品模板", icon: "icon-columns",
            sortNumber: 2230, parentId: goodsResourceId, url: "#station_productTemplates"
        })
    }

    //=========================================================================

    const orderResourceId = "CBB251C2-E90A-466B-8B7D-73FAC325D815";
    menuItem = await dc.resources.findOne({ id: orderResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: orderResourceId, name: "订单", icon: "icon-glass",
            sortNumber: 2300
        })
    }

    const orderListResourceId = "DD176E3A-88AB-42FF-BB73-668B8C7295FB";
    menuItem = await dc.resources.findOne(orderListResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: orderListResourceId, name: "订单列表", icon: "icon-align-justify",
            sortNumber: 2310, parentId: orderResourceId, url: "#shopping_orderList"
        })
    }

    //=========================================================================

    const promotionResourceId = "FF8ECCF4-1823-47A2-853C-FE31F455C4A0";
    menuItem = await dc.resources.findOne({ id: promotionResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: promotionResourceId, name: "营销", icon: "icon-bullhorn",
            sortNumber: 2400
        })
    }

    const promotionActivityResourceId = "CCB6BA6F-E4A6-4AD4-ADF9-63F15E4C3243";
    menuItem = await dc.resources.findOne(promotionActivityResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: promotionActivityResourceId, name: "促销活动", icon: "icon-retweet",
            sortNumber: 2410, parentId: promotionResourceId, url: "#shopping_promotion_activities"
        })
    }

    const couponResourceId = "5180BD51-15FE-4D34-8574-07D39E90DB8C";
    menuItem = await dc.resources.findOne(couponResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: couponResourceId, name: "优惠券", icon: "icon-paste",
            sortNumber: 2420, parentId: promotionResourceId, url: "#coupon_couponList"
        })
    }

    const couponCodeResourceId = "C0CE1B7F-9218-48BB-BD46-8EBE367E6721";
    menuItem = await dc.resources.findOne(couponCodeResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: couponCodeResourceId, name: "优惠码", icon: "icon-bookmark-empty",
            sortNumber: 2430, parentId: promotionResourceId, url: "#coupon_couponCodeList"
        })
    }

    //=========================================================================

    const memberResourceId = "01F84CE7-B21B-4B10-9375-00BCC2D46D37";
    menuItem = await dc.resources.findOne({ id: memberResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: memberResourceId, name: "会员", icon: "icon-group",
            sortNumber: 2500
        })
    }

    const memberListResourceId = "00766259-22F0-4061-8087-D0D9C6BB2110";
    menuItem = await dc.resources.findOne(memberListResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: memberListResourceId, name: "会员列表", icon: "icon-reorder",
            sortNumber: 2510, parentId: memberResourceId, url: "#member_memberList"
        })
    }

    //=========================================================================

    const settingResourceId = "D4D65F89-6CA3-4734-9984-A719D517C776";
    menuItem = await dc.resources.findOne({ id: settingResourceId });
    if (!menuItem) {
        await dc.createMenuItem({
            id: settingResourceId, name: "设置", icon: "icon-cog",
            sortNumber: 2600
        })
    }

    const freightSettingResourceId = "2E686236-7C5C-4D0B-A3E1-FB9F015F913F";
    menuItem = await dc.resources.findOne(freightSettingResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: freightSettingResourceId, name: "运费设置", icon: "icon-truck",
            sortNumber: 2610, parentId: settingResourceId, url: "#freight_solutionList"
        })
    }

    const paySettingResourceId = "74A8D1C5-1E0B-4978-94DA-14C5717132B4";
    menuItem = await dc.resources.findOne(paySettingResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: paySettingResourceId, name: "支付设置", icon: "icon-money",
            sortNumber: 2620, parentId: settingResourceId, url: "#shopping_paymentSetting"
        })
    }

    const weixinSettingResourceId = "8C68FCCA-EFFD-4062-8D58-04AF59BDBE0C";
    menuItem = await dc.resources.findOne(weixinSettingResourceId);
    if (!menuItem) {
        await dc.createMenuItem({
            id: weixinSettingResourceId, name: "公众号设置", icon: "icon-bullhorn",
            sortNumber: 2030, parentId: settingResourceId, url: "#weixin_setting"
        })
    }
    //=========================================================================

}

